package com.oocl.processdData.controller.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import com.oocl.processdData.cmdParse.CmdParse;
import com.oocl.processdData.cmdParse.impl.CmdParseImpl;
import com.oocl.processdData.controller.Controller;
import com.oocl.processdData.pojo.Customer;
import com.oocl.processdData.server.Server;
import com.oocl.processdData.service.CustomerService;
import com.oocl.processdData.service.impl.CustomerServiceImpl;
import com.oocl.processdData.util.CheckoutUtil;
import com.oocl.processdData.view.View;
import com.oocl.processdData.view.impl.ViewImpl;

public class ControllerImpl implements Controller {

	private View view;
	private CustomerService custService;
	private Server server;
	private CmdParse cmdParse;
	
	public ControllerImpl() {
		view = new ViewImpl();
		custService = new CustomerServiceImpl();
		cmdParse = new CmdParseImpl();
	}

	public void start() throws IOException{
		ArrayList<String> records = view.loadData("data.txt");
		ArrayList<Customer> orgCusts = custService.parseToCustomers(records);
		Set<Customer> checkedCusts = CheckoutUtil.checkAll(orgCusts);
		server = new Server();
		server.start();
		while(true){
			try{
				server.getOut().write("Input your command \r\n".getBytes());
				String inputMsg = custService.getInputMsg(server.getIn());
				String parseMsg = cmdParse.parse(inputMsg);
				custService.processCmd(parseMsg, checkedCusts, server.getOut());
			}catch(Exception e){
				server.getOut().write("command error \r\n".getBytes());
				server.getOut().write("Input your command \r\n".getBytes());
			}
			
		}
		
	}
	
}
